package com.zw.pdm.module.designtask.controller;

import com.zw.pdm.base.BaseController;
import com.zw.pdm.common.JsonResult;
import com.zw.pdm.module.designtask.mapper.DesignTaskMapper;
import com.zw.pdm.module.designtask.service.DsnTaskChangeService;
import com.zw.pdm.module.report.model.Project;
import com.zw.pdm.module.system.mapper.DeptMapper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description: 设计任务变更
 * @Author: China.wsg
 * @Date: 2020/11/13 16:02
 */
@Controller
@RequestMapping("/designTask/taskChange")
@SuppressWarnings("all")
public class DsnTaskChangeController extends BaseController {

    @Autowired
    private DsnTaskChangeService dsnTaskChangeService;
    @Autowired
    private DesignTaskMapper designTaskMapper;
    @Autowired
    private DeptMapper deptMapper;

    @GetMapping("/changePage")
    @RequiresPermissions({"dsn:taskCha:view"})
    public String changePage() {
        return "designtask/list/proChangePage";
    }

    /**
     * 项目变更列表
     *
     * @param project
     * @return
     */
    @ResponseBody
    @PostMapping("/changePage")
    @RequiresPermissions({"dsn:taskCha:view"})
    public JsonResult changePage(Project project) {
        startPage();
        List<Project> list = dsnTaskChangeService.findProjectByQuery(project);
        return jsonResult(list);
    }

    @GetMapping("/changeTaskPage/{proId}")
    @RequiresPermissions({"dsn:taskCha:view"})
    public String changeTaskPage(@PathVariable("proId") Integer proId, Model model) {
        Project project = designTaskMapper.findProjectById(proId);
        model.addAttribute("id", project.getId());
        model.addAttribute("code", project.getCode());
        model.addAttribute("name", project.getName());
        model.addAttribute("num", project.getNum());
        model.addAttribute("demander", project.getDemander());
        model.addAttribute("address", project.getAddress());
        model.addAttribute("comment", project.getComment());
        model.addAttribute("taskDepts", dsnTaskChangeService.findTaskDeptsByProId(proId));
        model.addAttribute("depts", deptMapper.findDsnDept());
        return "designtask/edit/proChangePage";
    }

    /**
     * 保存外观设计清单
     */
    @ResponseBody
    @PostMapping("/saveChange")
    @RequiresPermissions({"dsn:taskCha:edit"})
    public JsonResult saveChange(Integer proId,String comment, Integer[] oldId, Integer[] newId, Integer[] editFlg) {
        return jsonResult(dsnTaskChangeService.saveChange(proId, comment, oldId, newId, editFlg));
    }



}
